package com.fuyao.cloud.personnel.problem.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.api.feign.RemoteUserService;
import com.fuyao.cloud.admin.api.vo.UserVO;
import com.fuyao.cloud.personnel.api.entity.ProblemRectify;
import com.fuyao.cloud.personnel.api.entity.ProblemRectifyDetailed;
import com.fuyao.cloud.personnel.problem.mapper.ProblemRectifyDetailedMapper;
import com.fuyao.cloud.personnel.problem.service.ProblemRectifyDetailedService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 整改措施责任关联表
 *
 * @author pigx code generator
 * @date 2022-04-26 10:14:06
 */
@Service
@AllArgsConstructor
public class ProblemRectifyDetailedServiceImpl extends ServiceImpl<ProblemRectifyDetailedMapper, ProblemRectifyDetailed> implements ProblemRectifyDetailedService {

    private final RemoteUserService userService;

    @Override
    public boolean save(ProblemRectify rectify,List<Long> thoseResponsibleIds) {
        this.baseMapper.delete(Wrappers.<ProblemRectifyDetailed>query().lambda().eq(ProblemRectifyDetailed::getRectifyId, rectify.getId()));
        if(CollUtil.isNotEmpty(thoseResponsibleIds)){
            thoseResponsibleIds.forEach(userId -> {
                ProblemRectifyDetailed rectifyDetailed = new ProblemRectifyDetailed();
                rectifyDetailed.setDutyUserid(userId);
                UserVO userVO = userService.userAndDept(userId).getData();
                rectifyDetailed.setRectifyId(rectify.getId());
                rectifyDetailed.setRectifyNum(rectify.getNumber());
                rectifyDetailed.setDutyUsername(userVO.getRealName());
                rectifyDetailed.setDutyOuid(userVO.getDeptId());
                rectifyDetailed.setDutyName(userVO.getDeptName());
                rectifyDetailed.setDutyFullname(userVO.getDeptFullName());
                this.baseMapper.insert(rectifyDetailed);
            });
        }
        return Boolean.TRUE;
    }
    public boolean updateById(ProblemRectifyDetailed entity) {
        return super.updateById(entity);
    }

    @Override
    public IPage problemRectifyDetailedPage(Page page, QueryWrapper<ProblemRectifyDetailed> query) {
        query.eq("1","1");
        return this.baseMapper.problemRectifyDetailedPage(page,query);
    }
    private void saveOrUpdateBefore(ProblemRectifyDetailed entity,List<Integer> thoseResponsibleIds){
    }
}
